<template>
  <div class="login">
    <div class="login-conent">
      <el-card>
        <el-row>
          <el-col :span="12" class="form">
            <div ref="formTitle" class="form-title">
              <div class="form-title-active">免密登录</div>
              <div>密码登录</div>
            </div>
            <!-- 手机号免密登录 -->
            <div v-if="isTelLogin" class="tel-login-form">
              <el-form :model="telForm">
                <el-form-item>
                  <el-input
                    v-model="telForm.tel"
                    @blur="loginBtnHandler()"
                    placeholder="手机号"
                  />
                </el-form-item>
                <el-form-item>
                  <el-input
                    v-model="telForm.code"
                    placeholder="6位数字验证码"
                    @blur="loginBtnHandler()"
                  />
                  <span class="get-tel-code" @click="getTelCodeHandler()">
                    获取验证码
                  </span>
                </el-form-item>
                <el-form-item>
                  <el-button
                    :disabled="isLoginActive"
                    type="primary"
                    @click="formLoginHandler()"
                  >
                    登录/注册
                  </el-button>
                </el-form-item>
              </el-form>
            </div>
            <!-- 密码登录 -->
            <div v-else class="password-login-form">
              <el-form :model="accountForm">
                <el-form-item>
                  <el-input
                    v-model="accountForm.logName"
                    @blur="loginBtnHandler()"
                    placeholder="手机号/邮箱/用户名"
                  />
                </el-form-item>
                <el-form-item>
                  <el-input
                    v-model="accountForm.logPwd"
                    placeholder="密码"
                    @blur="loginBtnHandler()"
                  />
                  <span class="forget-password"> 忘记密码 </span>
                </el-form-item>
                <el-form-item>
                  <el-button
                    :disabled="isLoginActive"
                    type="primary"
                    @click="formLoginHandler()"
                  >
                    登录
                  </el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
          <!-- 微信二维码登录 -->
          <el-col :span="12" class="code">
            <div class="weixingLogin">微信扫一扫登录</div>
            <div><img src="../assets/weixing.jpg" /></div>
            <div class="msg">无需关注 立刻登录</div>
          </el-col>
        </el-row>
        <div class="tip">
          未注册手机验证后自动登录，注册即代表同意
          <a href="">服务条款</a> 和
          <a href="">隐私协议</a>
        </div>
      </el-card>
    </div>
    <blog-bottom-component></blog-bottom-component>
  </div>
</template>
<script>
import BlogBottomComponent from "../components/BlogBottomComponent.vue";
export default {
  data() {
    return {
      isTelLogin: true, // 登录方式
      isLoginActive: true, // 登录按钮状态
      telForm: { tel: "", code: "" },
      accountForm: { logName: "", logPwd: "" },
    };
  },
  components: {
    BlogBottomComponent,
  },
  methods: {
    // 登录方式切换
    formTitleActiveHandler() {
      this.$refs.formTitle.childNodes.forEach((element) => {
        let _this = this;
        element.onclick = function () {
          _this.$refs.formTitle.childNodes.forEach((item) => {
            item.setAttribute("class", "");
          });
          this.setAttribute("class", "form-title-active");
          if (this.innerText === "免密登录") {
            _this.isTelLogin = true;
          } else {
            _this.isTelLogin = false;
          }
        };
      });
    },
    // 获取验证码判断
    getTelCodeHandler() {
      if (this.telForm.tel === "") {
        this.$message.error("手机号不能为空！");
        //TODO 发送手机号短信验证
      }
      // 替换显示内容  60s后重新发送
    },
    // 登录按钮激活
    loginBtnHandler() {
      if (this.isTelLogin) {
        if (this.telForm.tel !== "" && this.telForm.code !== "") {
          this.isLoginActive = false;
        } else {
          this.isLoginActive = true;
        }
      } else {
        if (this.accountForm.logName !== "" && this.accountForm.logPwd !== "") {
          this.isLoginActive = false;
        } else {
          this.isLoginActive = true;
        }
      }
    },
    // 登录
    formLoginHandler() {
      //TODO 验证码比对
      this.$message("验证码比对，登录中...");
      window.sessionStorage.setItem("logStatus", "true");
      this.$router.push("/");
      window.location.reload();
    },
  },
  mounted() {
    this.formTitleActiveHandler();
  },
};
</script>
<style scoped>
.login {
  width: 100%;
  margin-top: 80px;
}
.login-conent {
  width: 100%;
  height: calc(100% - 200px);
  min-height: 660px;
  background-image: url(../assets/bg.png);
  background-size: cover;
  background-position: 50%;
  position: relative;
}
.el-card {
  width: 700px;
  height: 360px;
  position: absolute;
  right: 100px;
  top: 135px;
}
.form-title {
  display: flex;
}
.form-title > div {
  width: 120px;
  font-size: 17px;
  font-weight: bold;
  cursor: pointer;
  color: #666;
}
.form-title-active {
  color: black !important;
  position: relative;
}
.form-title-active::after {
  content: "";
  width: 30px;
  height: 2px;
  background-color: #409eff;
  position: absolute;
  top: 25px;
  left: 16%;
}

.tel-login-form,
.password-login-form {
  margin-top: 35px;
}
.el-button {
  width: 100%;
}
.get-tel-code,
.forget-password {
  cursor: pointer;
  position: relative;
  left: 75%;
  top: -40px;
}
.forget-password {
  top: 0;
  left: 83%;
}
.code .weixingLogin {
  font-size: 17px;
  font-weight: bold;
  text-align: center;
  margin-left: 15px;
}
.code img {
  width: 150px;
  position: relative;
  left: 30%;
  top: 20px;
}
.code .msg {
  text-align: center;
  position: relative;
  top: 50px;
  left: 10px;
  font-size: 12px;
}
.tip {
  font-size: 12px;
  background-color: #eee;
  height: 40px;
  line-height: 40px;
  width: 120%;
  position: relative;
  left: -20px;
  top: 13px;
  padding-left: 20px;
}
.tip a {
  color: black;
}
.tip a:hover {
  color: rgb(155, 3, 3);
}
</style>